package dfsketches.bicycles;

public class BicycleStack {

	public BicycleStack(int size) {
		queue = new int[size];
		head = -1;
	}
	
	public void push(int v) {
		if (head == queue.length - 1) increaseSize();
		queue[++head] = v;
	}
	
	public int pop() {
		return queue[head--];
	}
	
	public int top() {
		return queue[head];
	}
	
	private void increaseSize() {
		int newSize = queue.length * 2 + 1;
		int[] temp = new int[newSize];
		System.arraycopy(queue, 0, temp, 0, queue.length);
		queue = temp;
	}
	
	public void clear() {
		head = -1;	
	}
	
	public boolean isEmpty() {
		return head == -1;
	}

	public int size() {
		return head + 1;
	}
	
	public String toString() {
		if (size() == 0)
			return "0 : []";

		StringBuilder str = new StringBuilder();
		str.append(size() + " : [");
		for (int i = head; i > 0; i--) {
			str.append(queue[i] + ", ");
		}
		str.append(queue[0] + "]");
		return str.toString();
	}
	
	private int[] queue;
	private int head;
}